import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Scanner;
import java.util.TreeSet;

/**
 * 
 * MenuList stores objects of MenuItem class
 * 
 */
public class MenuList {
	public TreeSet<MenuItem> menuList;

	public MenuList() {
		menuList = new TreeSet<MenuItem>();
	}

	/**
	 * @return the menuList
	 */
	public TreeSet<MenuItem> getMenuList() {
		return menuList;
	}

	/**
	 * @param menuList
	 *            the menuList to set
	 */
	public void setMenuList(TreeSet<MenuItem> menuList) {
		this.menuList = menuList;
	}

	/**
	 * Writes the report to the file specified
	 * 
	 * @param File
	 *            to store into
	 * @param report
	 *            to be written in file
	 */
	public void writeToFile(String File, String report) {

		FileWriter fw;
		try {
			fw = new FileWriter(File);
			fw.write(report);
			fw.close();
		}
		// message and stop if file not found
		catch (FileNotFoundException fnf) {
			System.out.println(File + " not found ");
			System.exit(0);
		}
		// stack trace here because we don't expect to come here
		catch (IOException ioe) {
			ioe.printStackTrace();
			System.exit(1);
		}
	}

	/**
	 * Populate the array list from a text file
	 * 
	 * @param File
	 *            to read from
	 */

	public void readFile(String File)

	{
		try {
			File ordersFile = new File(File);
			Scanner scanner = new Scanner(ordersFile);
			while (scanner.hasNextLine()) {
				// read first line and process it
				String inputLine = scanner.nextLine();
				if (inputLine.length() != 0) {// ignored if blank line
					processLine(inputLine);
				}
			}
		}
		// if the file is not found, stop with system exit
		catch (FileNotFoundException fnf) {
			System.out.println(File + " not found ");
			System.exit(0);
		}

	}

	/**
	 * Processes line, extracts data, creates MenuItem object and adds to list
	 * Checks for non-numeric table number,quantity and missing items will still
	 * crash if name entered without a space
	 * 
	 * @param line
	 *            the line to be processed
	 */
	private void processLine(String line) {
		String strPrice = "";

		try {
			String parts[] = line.split(",");
			String dish = parts[0];
			String category = parts[1];
			String cuisine = parts[2];
			strPrice = parts[3];
			strPrice = strPrice.trim(); // remove any spaces
			double price = Double.parseDouble(strPrice);

			// create FoodOrder object and add to the list
			MenuItem m = new MenuItem(dish, category, cuisine, price);
			menuList.add(m);
			// System.out.println(m.getDishName());
		}

		// ignore lines in error and try and carry on

		// this catches trying to convert a String to an integer
		catch (NumberFormatException nfe) {
			String error = "Number conversion error in '" + line + "'  - "
					+ nfe.getMessage();
			System.out.println(error);
		}
		// this catches missing items if only one or two items
		// other omissions will result in other errors
		catch (Exception e) {
			String error = "Error  : '" + line + "' index position : "
					+ e.getMessage();
			System.out.println(error);
		}
	}

	// *************************************************************************************************************************************************************************
	// FUNCTIONS

	/** Function for return the Arabic Cuisine part of menu **/
	public String getArabicCuisine() {
		String arabicCuisine = "\n\t"
				+ String.format("%-38s", "Arabic Cuisine") + "\tPrice\n";
		String appeList = "\n   Appetizers\n\n";
		String saladList = "\n   Salads\n\n";
		String mainCList = "\n   Main Course\n\n";
		String dessertsList = "\n   Desserts\n\n";
		try {
			for (MenuItem dish : menuList) {
				if (dish.getCuisineName().equals("Arabic")) {
					if (dish.getCategoryName().equals("Appetizers"))
						appeList += String.format("%-45s", dish.getDishName())
								+ "\t"
								+ String.format("%-20s", dish.getPrice())
								+ "\n";
					else if (dish.getCategoryName().equals("Salads"))
						saladList += String.format("%-45s", dish.getDishName())
								+ "\t"
								+ String.format("%-20s", dish.getPrice())
								+ "\n";
					else if (dish.getCategoryName().equals("Main Course"))
						mainCList += String.format("%-45s", dish.getDishName())
								+ "\t"
								+ String.format("%-20s", dish.getPrice())
								+ "\n";
					else if (dish.getCategoryName().equals("Desserts"))
						dessertsList += String.format("%-45s",
								dish.getDishName())
								+ "\t"
								+ String.format("%-20s", dish.getPrice())
								+ "\n";
				}
			}
		} 
		
		catch (Exception e)
		{
			e.printStackTrace();
		}
		arabicCuisine += appeList + saladList + mainCList + dessertsList;
		return arabicCuisine;
	}

	/** Function for return the Chinese Cuisine part of menu **/
	public String getChineseCuisine() {
		String chineseCuisine = "\n\t"
				+ String.format("%-38s", "Chinese Cuisine") + "\tPrice";
		String appeList = "\n   Appetizers\n\n";
		String saladList = "\n   Salads\n\n";
		String mainCList = "\n   Main Course\n\n";
		String dessertsList = "\n   Desserts\n\n";
		try {
			for (MenuItem dish : menuList) {
				if (dish.getCuisineName().equals("Chinese")) {
					if (dish.getCategoryName().equals("Appetizers"))
						appeList += String.format("%-45s", dish.getDishName())
								+ "\t" + String.format("%-20s", dish.getPrice())
								+ "\n";
					else if (dish.getCategoryName().equals("Salads"))
						saladList += String.format("%-45s", dish.getDishName())
								+ "\t" + String.format("%-20s", dish.getPrice())
								+ "\n";
					else if (dish.getCategoryName().equals("Main Course"))
						mainCList += String.format("%-45s", dish.getDishName())
								+ "\t" + String.format("%-20s", dish.getPrice())
								+ "\n";
					else if (dish.getCategoryName().equals("Desserts"))
						dessertsList += String.format("%-45s", dish.getDishName())
								+ "\t" + String.format("%-20s", dish.getPrice())
								+ "\n";
				}
			}
		} 
		catch (Exception e) 
		{
			e.printStackTrace();
		}
		chineseCuisine += appeList + saladList + mainCList + dessertsList;
		return chineseCuisine;
	}

	/** Function for return the Indian Cuisine part of menu **/
	public String getIndianCuisine() {
		String indianCuisine = "\n\t"
				+ String.format("%-38s", "Indian Cuisine") + "\tPrice";
		String appeList = "\n   Appetizers\n\n";
		String saladList = "\n   Salads\n\n";
		String mainCList = "\n   Main Course\n\n";
		String dessertsList = "\n   Desserts\n\n";
		try {
			for (MenuItem dish : menuList) {
				if (dish.getCuisineName().equals("Indian")) {
					if (dish.getCategoryName().equals("Appetizers"))
						appeList += String.format("%-45s", dish.getDishName())
								+ "\t" + String.format("%-20s", dish.getPrice())
								+ "\n";
					else if (dish.getCategoryName().equals("Salads"))
						saladList += String.format("%-45s", dish.getDishName())
								+ "\t" + String.format("%-20s", dish.getPrice())
								+ "\n";
					else if (dish.getCategoryName().equals("Main Course"))
						mainCList += String.format("%-45s", dish.getDishName())
								+ "\t" + String.format("%-20s", dish.getPrice())
								+ "\n";
					else if (dish.getCategoryName().equals("Desserts"))
						dessertsList += String.format("%-45s", dish.getDishName())
								+ "\t" + String.format("%-20s", dish.getPrice())
								+ "\n";
				}
			}
		} 
		
		catch (Exception e) 
		{
			e.printStackTrace();
		}
		indianCuisine += appeList + saladList + mainCList + dessertsList;
		return indianCuisine;
	}

	/** Function for return the Italian Cuisine part of menu **/
	public String getItalianCuisine() {
		String italianCuisine = "\n\t"
				+ String.format("%-38s", "Italian Cuisine") + "\tPrice";
		String appeList = "\n   Appetizers\n\n";
		String saladList = "\n   Salads\n\n";
		String mainCList = "\n   Main Course\n\n";
		String dessertsList = "\n   Desserts\n\n";
		try {
			for (MenuItem dish : menuList) {
				if (dish.getCuisineName().equals("Italian")) {
					if (dish.getCategoryName().equals("Appetizers"))
						appeList += String.format("%-45s", dish.getDishName())
								+ "\t" + String.format("%-20s", dish.getPrice())
								+ "\n";
					else if (dish.getCategoryName().equals("Salads"))
						saladList += String.format("%-45s", dish.getDishName())
								+ "\t" + String.format("%-20s", dish.getPrice())
								+ "\n";
					else if (dish.getCategoryName().equals("Main Course"))
						mainCList += String.format("%-45s", dish.getDishName())
								+ "\t" + String.format("%-20s", dish.getPrice())
								+ "\n";
					else if (dish.getCategoryName().equals("Desserts"))
						dessertsList += String.format("%-45s", dish.getDishName())
								+ "\t" + String.format("%-20s", dish.getPrice())
								+ "\n";
				}
			}
		} 
		
		catch (Exception e) 
		{
			e.printStackTrace();
		}
		italianCuisine += appeList + saladList + mainCList + dessertsList;
		return italianCuisine;
	}

	/** Function for return the Mexican Cuisine part of menu **/
	public String getMexicanCuisine() {
		String mexicanCuisine = "\n\t"
				+ String.format("%-38s", "Mexican Cuisine") + "\tPrice";
		String appeList = "\n   Appetizers\n\n";
		String saladList = "\n   Salads\n\n";
		String mainCList = "\n   Main Course\n\n";
		String dessertsList = "\n   Desserts\n\n";
		try {
			for (MenuItem dish : menuList) {
				if (dish.getCuisineName().equals("Mexican")) {
					if (dish.getCategoryName().equals("Appetizers"))
						appeList += String.format("%-45s", dish.getDishName())
								+ "\t" + String.format("%-20s", dish.getPrice())
								+ "\n";
					else if (dish.getCategoryName().equals("Salads"))
						saladList += String.format("%-45s", dish.getDishName())
								+ "\t" + String.format("%-20s", dish.getPrice())
								+ "\n";
					else if (dish.getCategoryName().equals("Main Course"))
						mainCList += String.format("%-45s", dish.getDishName())
								+ "\t" + String.format("%-20s", dish.getPrice())
								+ "\n";
					else if (dish.getCategoryName().equals("Desserts"))
						dessertsList += String.format("%-45s", dish.getDishName())
								+ "\t" + String.format("%-20s", dish.getPrice())
								+ "\n";
				}
			}
		}
		
		catch (Exception e) 
		{
			e.printStackTrace();
		}
		mexicanCuisine += appeList + saladList + mainCList + dessertsList;
		return mexicanCuisine;
	}

	/**
	 * Prints the menu by taking individual cuisine menu combining it in a
	 * string and then printing it
	 */

	public String PrintMenu() {
		MenuList refer = new MenuList();
		String menuReport = "\t\t\tMenu\n";
		menuReport += getArabicCuisine() + "\n";
		menuReport += getChineseCuisine() + "\n";
		menuReport += getIndianCuisine() + "\n";
		menuReport += getItalianCuisine() + "\n";
		menuReport += getMexicanCuisine() + "\n";
		// System.out.println(menuReport);
		return menuReport;
	}
}